package hongvt.quanlynhanvien;

import hongvt.quanlynhanvien.adapter.CustomAdapter;
import hongvt.quanlynhanvien.database.DatabaseManager;
import hongvt.quanlynhanvien.model.Employee;
import hongvt.quanlynhanvien.model.Timekeeping;
import hongvt.quanlynhanvien.utils.Define;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import butterknife.InjectView;

public class TabEmpActivity extends AbstractActivity {
	@InjectView(R.id.listViewTabEmp)
	ListView listView;

	DatabaseManager dbManagement;
	List<Timekeeping> listTimekeepings;
	List<Employee> listemEmployees;
	CustomAdapter adapter;

	int daysQty;

	@Override
	protected int getCurrentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_tabemp;
	}

	@Override
	protected void afterInitView() {
		int month = 0;
		int year = 0;

		Bundle extras = getIntent().getExtras();

		if (extras != null) {
			month = Integer.parseInt(extras.getString(Define.MONTH));
			// Log.i("month", month);
			year = Integer.parseInt(extras.getString(Define.YEAR));
		}

		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(year, month, 1);
		// calendar.set(Calendar.YEAR,year);
		// calendar.set(Calendar.MONTH,month);
		daysQty = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		Log.i("dayYear", String.valueOf(year));
		Log.i("dayMonth", String.valueOf(month));
		Log.i("day", String.valueOf(daysQty));

		dbManagement = new DatabaseManager(context);
		listTimekeepings = dbManagement.getListTimekeepingByMonth(year, month);
		listemEmployees = dbManagement.getListEmployees();

		List<Employee> tempEmp = new ArrayList<Employee>();

		for (Employee emp : listemEmployees) {
			boolean hasExist = false;
			for (Timekeeping tk : listTimekeepings) {
				if (tk.getEmpId().equals(emp.getEmpId())) {
					hasExist = true;
					break;
				}
			}

			if (!hasExist) {
				tempEmp.add(emp);
			}
		}

		for (Employee emp : tempEmp) {
			Timekeeping tk = new Timekeeping();
			tk.setName(emp.getName());
			listTimekeepings.add(tk);
		}

		Log.i("count", String.valueOf(listTimekeepings.size()));

		adapter = new CustomAdapter(context, listTimekeepings, daysQty);

		listView.setAdapter(adapter);
	}

}
